import 'dart:convert';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_chip/config/Comm.dart';
import 'package:flutter_chip/utils/sp_utils.dart';
import 'package:flutter_chip/utils/toast_utils.dart';
import 'package:flutter_lifecycle_state/flutter_lifecycle_state.dart';

class BleSetting extends StatefulWidget {
  @override
  _BleSettingState createState() => _BleSettingState();
}

class _BleSettingState extends StateWithLifecycle<BleSetting> {
  Map<String, String> config = Map();
  var border = OutlineInputBorder(
    borderSide: BorderSide(),
    borderRadius: BorderRadius.all(Radius.circular(6)),
  );
  var hintStyle = TextStyle(fontSize: 12);

  var _dirUpController = TextEditingController();
  var _dirDownController = TextEditingController();
  var _dirLeftController = TextEditingController();
  var _dirRightController = TextEditingController();
  var _dirCenterController = TextEditingController();
  var _dirCLabelController = TextEditingController();

  var _recvlabel1Controller = TextEditingController();
  var _recvStart1Controller = TextEditingController();
  var _recvEnd1Controller = TextEditingController();
  var _recvlabel2Controller = TextEditingController();
  var _recvStart2Controller = TextEditingController();
  var _recvEnd2Controller = TextEditingController();

  var _btnlabelController1 = TextEditingController();
  var _btnSendController1 = TextEditingController();
  var _btnlabelController2 = TextEditingController();
  var _btnSendController2 = TextEditingController();
  var _btnlabelController3 = TextEditingController();
  var _btnSendController3 = TextEditingController();
  var _btnlabelController4 = TextEditingController();
  var _btnSendController4 = TextEditingController();
  var _btnlabelController5 = TextEditingController();
  var _btnSendController5 = TextEditingController();
  var _btnlabelController6 = TextEditingController();
  var _btnSendController6 = TextEditingController();
  var _btnlabelController7 = TextEditingController();
  var _btnSendController7 = TextEditingController();
  var _btnlabelController8 = TextEditingController();
  var _btnSendController8 = TextEditingController();
  var _btnlabelController9 = TextEditingController();
  var _btnSendController9 = TextEditingController();

  @override
  void initState() {
    super.initState();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitUp,
    ]);
    _configInit();
  }

  @override
  void dispose() {
    super.dispose();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeLeft,
      DeviceOrientation.landscapeLeft,
    ]);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 1.0,
        titleSpacing: 0.0,
        backgroundColor: Theme.of(context).canvasColor,
        title: Text(
          "参数设置",
          style: TextStyle(fontSize: 18),
        ),
        actions: <Widget>[
          FlatButton(
            onPressed: () => _saveConfig(),
            child: Text(
              "保存",
              style: TextStyle(fontSize: 18),
            ),
          )
        ],
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: EdgeInsets.all(10),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Text(
                "方向区：",
                style: TextStyle(fontSize: 16, color: Colors.grey),
              ),
              SizedBox(height: 10),
              _leftWidget(),
              Divider(),
              Text(
                "接收区：",
                style: TextStyle(fontSize: 16, color: Colors.grey),
              ),
              SizedBox(height: 10),
              _recvWidget(),
              Divider(),
              Text(
                "按键区：",
                style: TextStyle(fontSize: 16, color: Colors.grey),
              ),
              SizedBox(height: 10),
              _rightWidget(),
            ],
          ),
        ),
      ),
    );
  }

  ///中部接收区
  Widget _recvWidget() {
    double width =
        max((Comm.screenHeight - 20) / 2.5, (Comm.screenWidth - 20) / 2.5);
    return Wrap(
      spacing: 20,
      runSpacing: 20,
      children: <Widget>[
        SizedBox(
          width: width,
          child: TextField(
            controller: _recvlabel1Controller,
            decoration: InputDecoration(
              labelText: "显示标签",
              hintText: "请输入标签描述",
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _recvStart1Controller,
            decoration: InputDecoration(
              labelText: "开始索引",
              hintText: "请输入数据头",
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _recvEnd1Controller,
            decoration: InputDecoration(
              labelText: "结束索引",
              hintText: "请输入数据尾",
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: Container(),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _recvlabel2Controller,
            decoration: InputDecoration(
              labelText: "显示标签",
              hintText: "请输入标签描述",
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _recvStart2Controller,
            decoration: InputDecoration(
              labelText: "开始索引",
              hintText: "请输入数据头",
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _recvEnd2Controller,
            decoration: InputDecoration(
              labelText: "结束索引",
              hintText: "请输入数据尾",
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: Container(),
        ),
      ],
    );
  }

  ///左侧方向区
  Widget _leftWidget() {
    double width =
        max((Comm.screenHeight - 20) / 2.5, (Comm.screenWidth - 20) / 2.5);
    return Wrap(
      spacing: 20,
      runSpacing: 20,
      children: <Widget>[
        SizedBox(
          width: width,
          child: TextField(
            controller: _dirUpController,
            decoration: InputDecoration(
              labelText: "上键发送值",
              hintText: "请输入字母或数字，推荐4位",
              hintStyle: hintStyle,
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _dirDownController,
            decoration: InputDecoration(
              labelText: "下键发送值",
              hintText: "请输入字母或数字，推荐4位",
              contentPadding: EdgeInsets.all(10),
              hintStyle: hintStyle,
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _dirLeftController,
            decoration: InputDecoration(
              labelText: "左键发送值",
              hintText: "请输入字母或数字，推荐4位",
              hintStyle: hintStyle,
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _dirRightController,
            decoration: InputDecoration(
              labelText: "右键发送值",
              hintText: "请输入字母或数字，推荐4位",
              hintStyle: hintStyle,
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _dirCenterController,
            decoration: InputDecoration(
              labelText: "中间键发送值",
              hintText: "请输入字母或数字，推荐4位",
              hintStyle: hintStyle,
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _dirCLabelController,
            decoration: InputDecoration(
              labelText: "中间键标签",
              hintText: "请输入标签描述，两位汉字或字母",
              hintStyle: hintStyle,
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
      ],
    );
  }

  ///右侧按钮区
  Widget _rightWidget() {
    double width =
        max((Comm.screenHeight - 20) / 2.5, (Comm.screenWidth - 20) / 2.5);
    return Wrap(
      spacing: 20,
      runSpacing: 20,
      children: <Widget>[
        SizedBox(
          width: width,
          child: TextField(
            controller: _btnlabelController1,
            decoration: InputDecoration(
              labelText: "1号按钮标签",
              hintText: "请输入功能描述，推荐两位汉字",
              hintStyle: hintStyle,
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _btnSendController1,
            decoration: InputDecoration(
              labelText: "1号按钮发送值",
              hintText: "请输入字母或数字，推荐4位",
              contentPadding: EdgeInsets.all(10),
              hintStyle: hintStyle,
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _btnlabelController2,
            decoration: InputDecoration(
              labelText: "2号按钮标签",
              hintText: "请输入功能描述，推荐两位汉字",
              hintStyle: hintStyle,
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _btnSendController2,
            decoration: InputDecoration(
              labelText: "2号按钮发送值",
              hintText: "请输入字母或数字，推荐4位",
              contentPadding: EdgeInsets.all(10),
              hintStyle: hintStyle,
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _btnlabelController3,
            decoration: InputDecoration(
              labelText: "3号按钮标签",
              hintText: "请输入功能描述，推荐两位汉字",
              hintStyle: hintStyle,
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _btnSendController3,
            decoration: InputDecoration(
              labelText: "3号按钮发送值",
              hintText: "请输入字母或数字，推荐4位",
              contentPadding: EdgeInsets.all(10),
              hintStyle: hintStyle,
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _btnlabelController4,
            decoration: InputDecoration(
              labelText: "4号按钮标签",
              hintText: "请输入功能描述，推荐两位汉字",
              hintStyle: hintStyle,
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _btnSendController4,
            decoration: InputDecoration(
              labelText: "4号按钮发送值",
              hintText: "请输入字母或数字，推荐4位",
              contentPadding: EdgeInsets.all(10),
              hintStyle: hintStyle,
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _btnlabelController5,
            decoration: InputDecoration(
              labelText: "5号按钮标签",
              hintText: "请输入功能描述，推荐两位汉字",
              hintStyle: hintStyle,
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _btnSendController5,
            decoration: InputDecoration(
              labelText: "5号按钮发送值",
              hintText: "请输入字母或数字，推荐4位",
              contentPadding: EdgeInsets.all(10),
              hintStyle: hintStyle,
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _btnlabelController6,
            decoration: InputDecoration(
              labelText: "6号按钮标签",
              hintText: "请输入功能描述，推荐两位汉字",
              hintStyle: hintStyle,
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _btnSendController6,
            decoration: InputDecoration(
              labelText: "6号按钮发送值",
              hintText: "请输入字母或数字，推荐4位",
              contentPadding: EdgeInsets.all(10),
              hintStyle: hintStyle,
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _btnlabelController7,
            decoration: InputDecoration(
              labelText: "7号按钮标签",
              hintText: "请输入功能描述，推荐两位汉字",
              hintStyle: hintStyle,
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _btnSendController7,
            decoration: InputDecoration(
              labelText: "7号按钮发送值",
              hintText: "请输入字母或数字，推荐4位",
              contentPadding: EdgeInsets.all(10),
              hintStyle: hintStyle,
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _btnlabelController8,
            decoration: InputDecoration(
              labelText: "8号按钮标签",
              hintText: "请输入功能描述，推荐两位汉字",
              hintStyle: hintStyle,
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _btnSendController8,
            decoration: InputDecoration(
              labelText: "8号按钮发送值",
              hintText: "请输入字母或数字，推荐4位",
              contentPadding: EdgeInsets.all(10),
              hintStyle: hintStyle,
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _btnlabelController9,
            decoration: InputDecoration(
              labelText: "9号按钮标签",
              hintText: "请输入功能描述，推荐两位汉字",
              hintStyle: hintStyle,
              contentPadding: EdgeInsets.all(10),
              border: border,
            ),
          ),
        ),
        SizedBox(
          width: width,
          child: TextField(
            controller: _btnSendController9,
            decoration: InputDecoration(
              labelText: "9号按钮发送值",
              hintText: "请输入字母或数字，推荐4位",
              contentPadding: EdgeInsets.all(10),
              hintStyle: hintStyle,
              border: border,
            ),
          ),
        ),
      ],
    );
  }

  ///配置初始化
  void _configInit() async {
    config.addAll(await SpUtils.getBleConfig());

    _recvStart1Controller.value = TextEditingValue(text: config["recv_start1"]);
    _recvEnd1Controller.value = TextEditingValue(text: config["recv_end1"]);
    _recvlabel1Controller.value = TextEditingValue(text: config["label_recv1"]);
    _recvStart2Controller.value = TextEditingValue(text: config["recv_start2"]);
    _recvEnd2Controller.value = TextEditingValue(text: config["recv_end2"]);
    _recvlabel2Controller.value = TextEditingValue(text: config["label_recv2"]);

    _btnlabelController1.value = TextEditingValue(text: config["label_1"]);
    _btnlabelController2.value = TextEditingValue(text: config["label_2"]);
    _btnlabelController3.value = TextEditingValue(text: config["label_3"]);
    _btnlabelController4.value = TextEditingValue(text: config["label_4"]);
    _btnlabelController5.value = TextEditingValue(text: config["label_5"]);
    _btnlabelController6.value = TextEditingValue(text: config["label_6"]);
    _btnlabelController7.value = TextEditingValue(text: config["label_7"]);
    _btnlabelController8.value = TextEditingValue(text: config["label_8"]);
    _btnlabelController9.value = TextEditingValue(text: config["label_9"]);
    _btnSendController1.value = TextEditingValue(text: config["send_1"]);
    _btnSendController2.value = TextEditingValue(text: config["send_2"]);
    _btnSendController3.value = TextEditingValue(text: config["send_3"]);
    _btnSendController4.value = TextEditingValue(text: config["send_4"]);
    _btnSendController5.value = TextEditingValue(text: config["send_5"]);
    _btnSendController6.value = TextEditingValue(text: config["send_6"]);
    _btnSendController7.value = TextEditingValue(text: config["send_7"]);
    _btnSendController8.value = TextEditingValue(text: config["send_8"]);
    _btnSendController9.value = TextEditingValue(text: config["send_9"]);

    _dirUpController.value = TextEditingValue(text: config["dir_up"]);
    _dirDownController.value = TextEditingValue(text: config["dir_down"]);
    _dirLeftController.value = TextEditingValue(text: config["dir_left"]);
    _dirRightController.value = TextEditingValue(text: config["dir_right"]);
    _dirCenterController.value = TextEditingValue(text: config["dir_center"]);
    _dirCLabelController.value = TextEditingValue(text: config["dir_label"]);
  }

  void _saveConfig() async {
    config["dir_up"] = _dirUpController.text;
    config["dir_down"] = _dirDownController.text;
    config["dir_left"] = _dirLeftController.text;
    config["dir_right"] = _dirRightController.text;
    config["dir_center"] = _dirCenterController.text;
    config["dir_label"] = _dirCLabelController.text;

    config["recv_start1"] = _recvStart1Controller.text;
    config["recv_end1"] = _recvEnd1Controller.text;
    config["label_recv1"] = _recvlabel1Controller.text;
    config["recv_start2"] = _recvStart2Controller.text;
    config["recv_end2"] = _recvEnd2Controller.text;
    config["label_recv2"] = _recvlabel2Controller.text;

    config["label_1"] = _btnlabelController1.text;
    config["label_2"] = _btnlabelController2.text;
    config["label_3"] = _btnlabelController3.text;
    config["label_4"] = _btnlabelController4.text;
    config["label_5"] = _btnlabelController5.text;
    config["label_6"] = _btnlabelController6.text;
    config["label_7"] = _btnlabelController7.text;
    config["label_8"] = _btnlabelController8.text;
    config["label_9"] = _btnlabelController9.text;
    config["send_1"] = _btnSendController1.text;
    config["send_2"] = _btnSendController2.text;
    config["send_3"] = _btnSendController3.text;
    config["send_4"] = _btnSendController4.text;
    config["send_5"] = _btnSendController5.text;
    config["send_6"] = _btnSendController6.text;
    config["send_7"] = _btnSendController7.text;
    config["send_8"] = _btnSendController8.text;
    config["send_9"] = _btnSendController9.text;
    SpUtils.put(SpUtils.SP_BLE, jsonEncode(config));
    Toast.show("保存成功");
    Navigator.of(context).pop();
  }
}
